﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using KPIS.GERP.GBMS.BLL;
using Telerik.Web.UI;
using System.Web.UI;

namespace KPIS.GERP.GBMS.APP_UI
{
    public partial class BGM_Project_Control : System.Web.UI.Page
    {
        private ProjectControl bll = new ProjectControl();
        private ProjectControl_BLL oldbll = new ProjectControl_BLL();
        private ManageProject projectBLL = new ManageProject();
        private KPIS.GERP.GHRMS.BLL.EmployeeBLL empBLL = new KPIS.GERP.GHRMS.BLL.EmployeeBLL();
        private SystemControlBLL scBLL = new SystemControlBLL();
        private ComboBLL comboBLL = new ComboBLL();
        private Common.Main main = new Common.Main();
        private SsMng ssmng = new SsMng();

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (!IsPostBack)
            {
                hdHeaderID.Value = Request.QueryString["id"].ToString();
                hdBgmYear.Value = Request.QueryString["bgm_year"].ToString();

                lbJobSiteName.Text = "เงินกองกลางของ " + comboBLL.JobsiteInfo(Convert.ToInt32(hdHeaderID.Value)).Rows[0]["job_site_name"].ToString();
                liProject.Visible = false;
                liSubAct.Visible = false;
                liJobsite.Visible = false;
                liDepartment.Visible = false;
                LoadProjCombo();
                //var dtEmployee = empBLL.ListEmployeeBySeq(ssmng.EmpSeq);
                //if (dtEmployee.Rows.Count > 0 && 
                //    dtEmployee.Rows[0]["t_pos_seq"] != null && 
                //    dtEmployee.Rows[0]["t_pos_seq"].ToString() == "1") //====hardcode====
                //{
                //    rblType.Items[3].Enabled = true;
                //}
            }
        }

        protected void LoadRadGridProjectControl()
        {
            liProject.Visible = true;
            RadGridProjectControl.DataSource = bll.GetDataProjectExpense(hdBgmYear.Value, hdHeaderID.Value, ddlBgmProjSeq.SelectedValue);
            RadGridProjectControl.DataBind();
        }

        protected void RadGridProjectControl_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Filter" || e.CommandName == "Sort" || e.CommandName == "Page")
            {
                LoadRadGridProjectControl();
            }
        }

        protected void LoadRadGridSubActControl()
        {
            liSubAct.Visible = true;
            RadGridSubActControl.DataSource = bll.GetDataProjectExpenseControl(hdBgmYear.Value, hdHeaderID.Value, "1");
            RadGridSubActControl.DataBind();
        }

        protected void RadGridSubActControl_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Filter" || e.CommandName == "Sort" || e.CommandName == "Page")
            {
                LoadRadGridSubActControl();
            }
        }

        protected void LoadRadGridJobsiteControl()
        {
            liJobsite.Visible = true;
            RadGridJobsiteControl.DataSource = bll.GetDataProjectExpenseControl(hdBgmYear.Value, hdHeaderID.Value, "2");
            RadGridJobsiteControl.DataBind();
        }

        protected void RadGridJobsiteControl_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Filter" || e.CommandName == "Sort" || e.CommandName == "Page")
            {
                LoadRadGridJobsiteControl();
            }
        }

        protected void LoadRadGridDepartmentControl()
        {
            liDepartment.Visible = true;
            RadGridDepartmentControl.DataSource = bll.GetDataProjectExpenseControl(hdBgmYear.Value, hdHeaderID.Value, "3");
            RadGridDepartmentControl.DataBind();
        }

        private void LoadProjCombo()
        {
            DataTable dt = comboBLL.ListProject(hdBgmYear.Value, hdHeaderID.Value, null);
            ddlBgmProjSeq.DataSource = dt;
            ddlBgmProjSeq.DataBind();

            ddlBgmProjSeq.Items.Insert(0, new RadComboBoxItem("----- โครงการทั้งหมด -----", ""));
            ddlBgmProjSeq.SelectedIndex = 0;

            if (ddlBgmProjSeq.Items.Count > 50)
            {
                Filter.Attributes.Add("onclick", "Confirm()");
            }
            else
            {
                Filter.Attributes.Clear();
            }
        }

        protected void RadGridDepartmentControl_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Filter" || e.CommandName == "Sort" || e.CommandName == "Page")
            {
                LoadRadGridDepartmentControl();
            }
        }

        protected void rblType_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlBgmProjSeq.Visible = (rblType.SelectedValue == "1" || rblType.SelectedValue == "4");
        }

        protected void Filter_Click(object sender, EventArgs e)
        {
            switch (rblType.SelectedValue)
            {
                case "1":
                    LoadRadGridProjectControl();
                    LoadRadGridSubActControl();
                    liJobsite.Visible = false;
                    liDepartment.Visible = false;
                    break;
                case "2":
                    LoadRadGridSubActControl();
                    LoadRadGridJobsiteControl();
                    liProject.Visible = false;
                    liDepartment.Visible = false;
                    break;
                case "3":
                    LoadRadGridJobsiteControl();
                    LoadRadGridDepartmentControl();
                    liProject.Visible = false;
                    liSubAct.Visible = false;
                    break;
                case "4":
                    LoadRadGridProjectControl();
                    //LoadRadGridSubActControl();
                    liSubAct.Visible = false;
                    liJobsite.Visible = false;
                    liDepartment.Visible = false;
                    break;
            }
        }

        protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            if (RadGridDepartmentControl.Visible)
            {
                LoadRadGridDepartmentControl();
            }

            if (RadGridJobsiteControl.Visible)
            {
                LoadRadGridJobsiteControl();
            }

            if (RadGridProjectControl.Visible)
            {
                LoadRadGridProjectControl();
            }

            if (RadGridSubActControl.Visible)
            {
                LoadRadGridSubActControl();
            }
        }

        protected void RadGridSubActControl_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                string strType = rblType.SelectedValue;
                ((ImageButton)((GridDataItem)e.Item)["button"].FindControl("cmdPlus")).Visible = strType == "1" && ssmng.WorkingJobSiteSeq == scBLL.JobSiteStrategyID();
                ((ImageButton)((GridDataItem)e.Item)["button"].FindControl("cmdMinus")).Visible = strType == "2";
            }
        }

        protected void RadGridJobsiteControl_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                string strType = rblType.SelectedValue;
                ((ImageButton)((GridDataItem)e.Item)["button"].FindControl("cmdPlus")).Visible = strType == "2" && ssmng.WorkingJobSiteSeq == scBLL.JobSiteStrategyID();
                ((ImageButton)((GridDataItem)e.Item)["button"].FindControl("cmdMinus")).Visible = strType == "3";
            }
        }
    }
}